import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:study_flutter/dyc/utils/utils.dart';
import 'package:study_flutter/dyc/view/DycAppointmentPage.dart';
import 'package:study_flutter/dyc/view/DycFindPage.dart';
import 'package:study_flutter/dyc/view/DycTimetablePage.dart';
import 'package:study_flutter/dyc/view/PersonalCenterPage.dart';

import 'view/DYCHomePage.dart';

class DycMain extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new MainScreenState();
  }

}

class MainScreenState extends State<DycMain>
  with AutomaticKeepAliveClientMixin {
  PageController _pageController = PageController();

  /// 当前选中的索引
  int _selectedIndex = 0; // 当前选中的索引

  /// tabs的名字
  final bottomBarTitles = ["首页", "课表", "约课", "发现", "我的"];
  /// tabs的图标
  final selectIcon = ["ic_homepage", "btn_kecheng_s", "ic_appointment", "btn_find_s", "ic_me"];
  // 未选中icon
  final normalIcon = ["ic_home_n", "btn_kecheng_n", "ic_appointment_n", "btn_find_n", "ic_me_n"];
  //未选中icon

  /// 五个Tabs的内容
  var pages = <Widget>[
    DYCHomeRoute(),
    DycTimetablePagePage(),
    DycAppointmentPage(),
    DycFindPage(),
    PersonalCenterPage(),
  ];

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return WillPopScope(
      onWillPop: _onWillPop, // onWillPop 就表示当前页面将退出
      child: Scaffold(
        body: PageView.builder(
          itemBuilder: (context, index) => pages[index],
          itemCount: pages.length,
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            setState(() {
              _selectedIndex = index;
            });
          },
        ),
        bottomNavigationBar: BottomNavigationBar(
          selectedItemColor:Colors.red,
          unselectedItemColor: Colors.grey,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: buildImage(0, "ic_homepage"), //Icon(Icons.home),
              title: Text(bottomBarTitles[0]),
            ),
            BottomNavigationBarItem(
              icon: buildImage(1, "btn_kecheng_s"), //Icon(Icons.assignment),
              title: Text(bottomBarTitles[1]),
            ),
            BottomNavigationBarItem(
              icon: buildImage(2, "ic_appointment"), //Icon(Icons.chat),
              title: Text(bottomBarTitles[2]),
            ),
            BottomNavigationBarItem(
              icon: buildImage(3, "btn_find_s"), //Icon(Icons.assignment),
              title: Text(bottomBarTitles[3])
              ,
            ),
            BottomNavigationBarItem(
              icon: buildImage(4, "ic_me"), //Icon(Icons.book),
              title: Text(bottomBarTitles[4]),
            ),
          ],
          type: BottomNavigationBarType.fixed, // 设置显示模式
          currentIndex: _selectedIndex, // 当前选中项的索引
          onTap: _onItemTapped, // 选择的处理事件 选中变化回调函数
        ),
      ),
    );
  }


  void _onItemTapped(int index) {
    _pageController.jumpToPage(index);
  }

  /// tabs 底总的图片
  Widget buildImage(index, iconPath) {
    return Image.asset(
      _selectedIndex == index
        ?Utils.getImgPath(selectIcon[index])
        :Utils.getImgPath(normalIcon[index]),
      width: 22,
      height: 22,
    );
  }

  Future<bool> _onWillPop() {
    return showDialog(
      context: context,
      builder: (context) => new AlertDialog(
        title: new Text('提示'),
        content: new Text('确定退出应用吗？'),
        actions: <Widget>[
          new FlatButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: new Text('再看一会', style: TextStyle(color: Colors.cyan)),
          ),
          new FlatButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: new Text('退出', style: TextStyle(color: Colors.cyan)),
          ),
        ],
      ),
    ) ??
      false;
  }
}